package com.example.tigering.controller;

import com.example.tigering.dao.MaterialMapper;
import com.example.tigering.domain.Material;
import com.example.tigering.domain.MaterialQC;
import com.example.tigering.domain.QueryCondition;
import com.example.tigering.service.CodeProducer;
import com.example.tigering.service.RedisCodeGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Author: xwg
 * @CreateDate: 2023/8/21
 */

@RestController
// controller  responseBody
@RequestMapping("/time")
public class TimeController {
//     YD20230823000001
//     YD20230823000002
//     YD20230823000003
//     YD20230823000004
//     RK20230823000001

    @GetMapping("/like")
    public String like() {
//        System.out.println("---------");
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd 'Time' HH:mm:ss"));
    }
    @GetMapping("/like2")
    public String like2() {
//        System.out.println("---------");
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd 'Timing' HH:mm:ss"));
    }

    @Autowired
    private MaterialMapper materialMapper;

    @Autowired
    private CodeProducer codeProducer;
    @PostMapping("/insertxxx")
    public Material insert(@RequestBody Material material) {

        material.setMaterialCode(codeProducer.nextCode("MA"));
        System.out.println(material);
        int insert = materialMapper.insert(material);

        return material;
    }

    @PostMapping("/insertyyyy")
    public List<Material> insertMany(@RequestBody List<Material> materials) {

        System.out.println(materials);
        int insert = materialMapper.insertMany(materials);

        return materials;
    }

    @PutMapping("/modify")
    public Material modify(@RequestBody Material material) {

        int update = materialMapper.update(material);

        return material;
    }

    //    /find/002
    @GetMapping("/find/{id}")
    public Material find(@PathVariable("id") String id) {
        System.out.println(id);
        return materialMapper.findById(id);
    }

    @DeleteMapping("/remove/{id}")
    public String remove(@PathVariable("id") String id) {
        int i = materialMapper.deleteById(id);
        return id;
    }

    @PostMapping("/query")
    public List<Material> query(@RequestBody QueryCondition queryCondition) {
        Integer pageStart = (queryCondition.getPageNum() - 1) * queryCondition.getPageSize();
        List<Material> query = materialMapper.query(pageStart, queryCondition.getPageSize(), queryCondition.getPrice());
        return query;
    }

    // 原生mybatis + pagehelper 进行分页
//    mybatis plus 自己有分页器
    @PostMapping("/dynamicQuery")
    public PageInfo<Material> dynamicQuery(@RequestBody MaterialQC materialQC) {
//        limit 子句
        PageHelper.startPage(materialQC.getPageNum(),materialQC.getPageSize());
        List<Material> materials = materialMapper.dynamicQuery(materialQC);
        return new PageInfo<Material>(materials);
    }
//    访问redis  第一步 pom 添加依赖
//    	<dependency>
//			<groupId>org.springframework.boot</groupId>
//			<artifactId>spring-boot-starter-data-redis</artifactId>
//		</dependency>
//    第二步 properties yml 中配置
//    第三步 java文件里 autowire
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @GetMapping("/redisTest")
    public String redisTest(){
//
        ValueOperations<String, String> ops =
                stringRedisTemplate.opsForValue();
        ops.set("yourname","xwg");
//        String yourname = "xwg"
        return "xwg";
    }
}
